<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Services::Discovery::ByFlux</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Services::Discovery::ByFlux
                <span class="parent">&lt;
                    <a href="../Discovery.html">Hoodoo::Services::Discovery</a>
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../../files/lib/hoodoo/services/discovery/discoverers/by_flux_rb.html">lib/hoodoo/services/discovery/discoverers/by_flux.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>Discover resource endpoint locations via Alchemy Flux.</p>

<p>For Flux, it&#39;s less about discovery as it is about convention and
announcing. We have to set some system variables when the application
starts up, <em>before</em> the <a href="../../../Rack.html">Rack</a> `run`
call gets as far as the Alchemy Flux server&#39;s implementation - in
practice this means announcement needs to happen from the <a
href="../../../Hoodoo.html">Hoodoo</a> middleware&#39;s constructor,
synchronously. The environment variables tell Flux about this local
service&#39;s URI-located endpoints and derive a consistent, replicable
&#39;service name&#39; from the resources which the service implements.</p>

<p>Once all that is set up, the local Alchemy instance knows how to listen for
relevant messages for &#39;this&#39; service on the queue and <a
href="../../../Hoodoo.html">Hoodoo</a> in &#39;this&#39; service knowns
which resources are local, or which are remote; and it knows that Flux is
able in turn to use URI-based to-resource communications for inter-resource
calls without any further explicit discovery within <a
href="../../../Hoodoo.html">Hoodoo</a> beyond simply saying “here&#39;s the
AMQP Flux endpoint class”.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>A</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-announce_remote">announce_remote</a>
              </li>
          </ul>
        </dd>
        <dt>D</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-discover_remote">discover_remote</a>
              </li>
          </ul>
        </dd>
    </dl>










    <!-- Methods -->
      <div class="sectiontitle">Instance Protected methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-announce_remote">
              <b>announce_remote</b>( resource, version, options = {} )
            <a href="../../../../classes/Hoodoo/Services/Discovery/ByFlux.html#method-i-announce_remote" name="method-i-announce_remote" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Announce the location of an instance to Alchemy Flux.</p>

<p>Call via Hoodoo::Services::Discovery::Base#announce.</p>
<dl class="rdoc-list note-list"><dt><code>resource</code>
<dd>
<p>Passed to <a
href="ByFlux.html#method-i-discover_remote">discover_remote</a>.</p>
</dd><dt><code>version</code>
<dd>
<p>Passed to <a
href="ByFlux.html#method-i-discover_remote">discover_remote</a>.</p>
</dd><dt><code>options</code>
<dd>
<p>See below.</p>
</dd></dl>

<p>The Options hash informs the announcer of the intended endpoint base URI
for the resource and also, where available, provides a head-up on the full
range of resource <em>names</em> that will be present in this single
service application (see <a
href="../Service.html#method-c-comprised_of">Hoodoo::Services::Service.comprised_of</a>).
Keys MUST be Symbols. Associated required values are:</p>
<dl class="rdoc-list note-list"><dt><code>services</code>
<dd>
<p>Array of <a href="ForLocal.html">Hoodoo::Services::Discovery::ForLocal</a>
instances describing available resources in this local service.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-announce_remote_source')" id="l_method-i-announce_remote_source">show</a>
              </p>
              <div id="method-i-announce_remote_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_flux.rb, line 58</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">announce_remote</span>( <span class="ruby-identifier">resource</span>, <span class="ruby-identifier">version</span>, <span class="ruby-identifier">options</span> = {} )

  <span class="ruby-identifier">alchemy_resource_paths</span> = <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;ALCHEMY_RESOURCE_PATHS&#39;</span> ]
  <span class="ruby-identifier">alchemy_service_name</span>   = <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;ALCHEMY_SERVICE_NAME&#39;</span>   ]

  <span class="ruby-comment"># Under Flux, we &quot;announce&quot; via a local environment variable when</span>
  <span class="ruby-comment"># this service awakens which tells Flux what to listen for on the</span>
  <span class="ruby-comment"># AMQP queue.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-comment"># Since inbound HTTP calls into the architecture are based on URIs</span>
  <span class="ruby-comment"># and paths, there needs to be a mapping at that point to queue</span>
  <span class="ruby-comment"># endpoints. Historically Hoodoo adopted an (in hindsight, unwise)</span>
  <span class="ruby-comment"># approach of &quot;/v&lt;version&gt;/&lt;pluralised_resource&gt;&quot; c.f. Rails,</span>
  <span class="ruby-comment"># rather than just &quot;/&lt;version&gt;/&lt;resource&gt;&quot; - e.g. there was</span>
  <span class="ruby-comment"># &quot;/v1/members&quot; instead of &quot;/1/Member&quot;. This means things like the</span>
  <span class="ruby-comment"># &quot;ByConvention&quot; discoverer have to use pluralisation rules and</span>
  <span class="ruby-comment"># exceptions. It&#39;s messy.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-comment"># To clean things up, the work on Alchemy Flux sets up *two* paths</span>
  <span class="ruby-comment"># in Hoodoo - the old one for backwards compatibility, and a new</span>
  <span class="ruby-comment"># one of the above simpler form. Now it&#39;s easy to go from version</span>
  <span class="ruby-comment"># and resource name to path or back internally with no mappings.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-keyword">if</span> ( <span class="ruby-identifier">alchemy_resource_paths</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span>
       <span class="ruby-identifier">alchemy_resource_paths</span>.<span class="ruby-identifier">strip</span>.<span class="ruby-identifier">empty?</span> )

    <span class="ruby-identifier">services</span> = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:services</span> ] <span class="ruby-operator">||</span> []
    <span class="ruby-identifier">paths</span>    = []

    <span class="ruby-identifier">services</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">service</span> <span class="ruby-operator">|</span>
      <span class="ruby-identifier">custom_path</span>   = <span class="ruby-identifier">service</span>.<span class="ruby-identifier">base_path</span>
      <span class="ruby-identifier">de_facto_path</span> = <span class="ruby-identifier">service</span>.<span class="ruby-identifier">de_facto_base_path</span>

      <span class="ruby-identifier">paths</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">custom_path</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">de_facto_path</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;ALCHEMY_RESOURCE_PATHS&#39;</span> ] = <span class="ruby-identifier">paths</span>.<span class="ruby-identifier">join</span>( <span class="ruby-string">&#39;,&#39;</span> )
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> ( <span class="ruby-identifier">alchemy_service_name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span>
       <span class="ruby-identifier">alchemy_service_name</span>.<span class="ruby-identifier">strip</span>.<span class="ruby-identifier">empty?</span> )
    <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;ALCHEMY_SERVICE_NAME&#39;</span> ] = <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Services</span><span class="ruby-operator">::</span><span class="ruby-constant">Middleware</span><span class="ruby-operator">::</span><span class="ruby-identifier">service_name</span>()
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">discover_remote</span>( <span class="ruby-identifier">resource</span>, <span class="ruby-identifier">version</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-discover_remote">
              <b>discover_remote</b>( resource, version )
            <a href="../../../../classes/Hoodoo/Services/Discovery/ByFlux.html#method-i-discover_remote" name="method-i-discover_remote" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Discover the location of an instance.</p>

<p>Returns a <a href="ForAMQP.html">Hoodoo::Services::Discovery::ForAMQP</a>
instance if the endpoint is found, else <code>nil</code>.</p>

<p>Call via Hoodoo::Services::Discovery::Base#announce.</p>
<dl class="rdoc-list note-list"><dt><code>resource</code>
<dd>
<p>Passed to <a
href="ByFlux.html#method-i-discover_remote">discover_remote</a>.</p>
</dd><dt><code>version</code>
<dd>
<p>Passed to <a
href="ByFlux.html#method-i-discover_remote">discover_remote</a>.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-discover_remote_source')" id="l_method-i-discover_remote_source">show</a>
              </p>
              <div id="method-i-discover_remote_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_flux.rb, line 115</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">discover_remote</span>( <span class="ruby-identifier">resource</span>, <span class="ruby-identifier">version</span> )
  <span class="ruby-identifier">de_facto_path</span> = <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Services</span><span class="ruby-operator">::</span><span class="ruby-constant">Middleware</span><span class="ruby-operator">::</span><span class="ruby-identifier">de_facto_path_for</span>(
    <span class="ruby-identifier">resource</span>,
    <span class="ruby-identifier">version</span>
  )

  <span class="ruby-keyword">return</span> <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Services</span><span class="ruby-operator">::</span><span class="ruby-constant">Discovery</span><span class="ruby-operator">::</span><span class="ruby-constant">ForAMQP</span>.<span class="ruby-identifier">new</span>(
    <span class="ruby-identifier">resource</span><span class="ruby-operator">:</span> <span class="ruby-identifier">resource</span>,
    <span class="ruby-identifier">version</span><span class="ruby-operator">:</span>  <span class="ruby-identifier">version</span>
  )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
